package haizhi.fruitmall.action;

import haizhi.fruitmall.constant.OrderGoodsMsg;
import haizhi.fruitmall.constant.OrderMsg;
import haizhi.fruitmall.constant.ResultConstant;
import haizhi.fruitmall.constant.UserMsg;
import haizhi.fruitmall.pojo.po.Shop;
import haizhi.fruitmall.pojo.po.UserShop;
import haizhi.fruitmall.service.IndexService;
import haizhi.fruitmall.service.ShopService;
import haizhi.fruitmall.service.UserService;
import haizhi.fruitmall.service.UserShopService;
import haizhi.fruitmall.util.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/shop")
public class ShopController {

	@Autowired
	private IndexService indexService;

	@Autowired
	private ShopService shopService;

	@Autowired
	private UserShopService userShopService;

	@Autowired
	private UserService userService;

	/**
	 * 在用户的购物车中添加商品以及购买数量
	 * 注意：如果添加的商品在购物车中已有相同的商品，则应该将它们合二为一，修改数量即可
	 * @param map goos_id  和     purchase_num
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ResponseBody
	public Map addGoodsToShoppingCart(@RequestBody Map map) {
		Map messages = new HashMap();

		// 获得登录的用户名
		String user_name = (String) map.get(UserMsg.USER_NAME);
		// 获得商品编号
		String goods_id = (String) map.get(OrderMsg.GOODS_ID);
		// 该商品的数量
		Integer purchase_num = (Integer) map.get(OrderGoodsMsg.PURCHASE_NUM);

		// 根据登录用户名获得user_id
		Integer user_id = userService.getUserIdByUsername(user_name);

		// 用户购物车编号
		String shop_id = "";

		// 查看登录是否已有购物车id,有,不需要创建id,否则创建
		UserShop user_shop = userShopService.getUserShop(user_id);
		if (user_shop == null) {
			shop_id = RandomUtils.generateString(13);// 生成订单id(年份+9位随机数=13位流水号)
			// 防止生成的购物车编号重复
			List<UserShop> userShops = userShopService.getUserShops(shop_id);
			while (userShops.size() != 0) {
				shop_id = RandomUtils.generateString(13);
				userShops = userShopService.getUserShops(shop_id);
			}
			// 为用户添加一个购物车编号
			UserShop userShop = new UserShop();
			userShop.setUserId(user_id);
			userShop.setShopId(shop_id);
			userShopService.addUserShop(userShop);
		} else {
			shop_id = user_shop.getShopId();
		}

		// 往购物车中添加商品时要注意添加的商品在购物车中是否已经存在，是，修改数量
		List<Shop> shops = shopService.getShopsByShopId(shop_id);
		for (Shop shop : shops) {
			if (shop.getGoodsId().equals(goods_id)) {// 查看该购物车中是否已经有该商品
				// 存在，修改数量
				Shop shopupdate = new Shop();
				shopupdate.setPurchaseNum(purchase_num + shop.getPurchaseNum());
				shopService.updatePurchaseNum(shopupdate, shop_id,
						Long.parseLong(goods_id));
			}
		}

		// 创建购物车
		Shop shop = new Shop();
		shop.setShopId(shop_id);
		shop.setGoodsId(Long.parseLong(goods_id));
		shop.setPurchaseNum(purchase_num);

		// 添加购物车
		shopService.addShop(shop);

		messages.put(ResultConstant.RESULT, ResultConstant.SUCCESS);
		return messages;
	}

	/**
	 * 删除购物车中的商品
	 * @param map
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/delete", method = RequestMethod.DELETE, produces = "application/json;charset=UTF-8")
	@ResponseBody
	public Map deleteGoodsToShoppingCart(@RequestBody Map map) {
		Map messages = new HashMap();
		// 获得登录的用户名
		String user_name = (String) map.get(UserMsg.USER_NAME);
		// 获得商品编号
		String goods_id = (String) map.get(OrderMsg.GOODS_ID);

		// 根据登录用户名获得user_id
		Integer user_id = userService.getUserIdByUsername(user_name);

		// 获得用户的购物车
		UserShop user_shop = userShopService.getUserShop(user_id);

		// 删除购物车中的商品
		shopService
				.deleteGoods(user_shop.getShopId(), Long.parseLong(goods_id));
		messages.put(ResultConstant.RESULT, ResultConstant.SUCCESS);
		return messages;
	}

	/**
	 * 修改购物车中的商品数量
	 * @param map
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/update", method = RequestMethod.PUT, produces = "application/json;charset=UTF-8")
	@ResponseBody
	public Map updateGoodsToShoppingCart(@RequestBody Map map) {
		Map messages = new HashMap();
		// 要修改的数量
		Integer purchase_num = (Integer) map.get(OrderGoodsMsg.PURCHASE_NUM);
		// 获得登录的用户名
		String user_name = (String) map.get(UserMsg.USER_NAME);
		// 获得商品编号
		String goods_id = (String) map.get(OrderMsg.GOODS_ID);

		// 根据登录用户名获得user_id
		Integer user_id = userService.getUserIdByUsername(user_name);

		// 获得用户的购物车
		UserShop user_shop = userShopService.getUserShop(user_id);

		Shop shop = new Shop();
		shop.setPurchaseNum(purchase_num);

		shopService.updatePurchaseNum(shop, user_shop.getShopId(),
				Long.parseLong(goods_id));
		messages.put(ResultConstant.RESULT, ResultConstant.SUCCESS);
		return messages;
	}

	/**
	 * 获得购物车信息
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/get", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	@ResponseBody
	public Map getGoodsToShoppingCart(@RequestParam Map map) {
		Map messages = new HashMap();
		// 获得登录的用户名
		String user_name = (String) map.get(UserMsg.USER_NAME);

		// 根据登录用户名获得user_id
		Integer user_id = userService.getUserIdByUsername(user_name);

		// 获得用户的购物车
		UserShop user_shop = userShopService.getUserShop(user_id);

		if (user_shop == null) {// 还未添加任何商品到购物车,则未对该用户创建购物车
			messages.put(ResultConstant.RESULT, ResultConstant.NULL);
			return messages;
		}

		// 查询购物车中的商品
		List<Shop> shops = shopService.getShopsByShopId(user_shop.getShopId());
		if (shops.size() == 0) { // 购物车中还未添加任何商品
			messages.put(ResultConstant.RESULT, ResultConstant.NULL);
			return messages;
		}

		// 用于封装该购物车中所有的商品信息
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		// 用于封装该购物车中每条商品的信息(包括商品,商品参数,商品购买数量)
		Map<String, Object> goodsAndGoodsparamsMap = null;
		for (Shop shop : shops) {
			Long goods_id = shop.getGoodsId();
			Integer purchase_num = shop.getPurchaseNum();
			// 通过商品id获取商品及商品参数
//			GoodsParamVO goodsParamVO = indexService.getGoodsParams(goods_id);
			goodsAndGoodsparamsMap = new HashMap<String, Object>();
			// 添加商品及商品参数
//			goodsAndGoodsparamsMap.put("goods_and_params", goodsParamVO);
			// 该购物车中商品的个数
			goodsAndGoodsparamsMap
					.put(OrderGoodsMsg.PURCHASE_NUM, purchase_num);

			// 添加一组商品的所有信息
			list.add(goodsAndGoodsparamsMap);
		}
		messages.put(ResultConstant.RESULT, list);
		return messages;
	}
}
